<?php 

class SanPham
{
	public $sp_ma;
	public $sp_ten;
	public $sp_ngay_tao;
	public $sp_mo_ta;
	public $sp_don_vi_tinh;
	public $sp_don_gia;
	public $sp_hinh_anh;
	public $sp_so_luong;
	public $sp_so_luong_ban;
	public $sp_tinh_trang;
	public $sp_tong_luot_danh_gia;
	public $sp_tong_so_sao;
	public $sp_ma_gian_hang;
	public $sp_ten_gian_hang;
	public $sp_username_chu_giang_hang;
	public $sp_ma_danh_muc ;
	public $sp_ten_danh_muc;
	public $sp_ten_tag;
	
	public static function PartseToSanPham($row)
	{
		$spDTO = new SanPham();
		$spDTO->sp_ma = $row["sp_ma"];
		$spDTO->sp_ten = $row["sp_ten"];
		$spDTO->sp_ngay_tao = $row["sp_ngay_tao"];
		$spDTO->sp_mo_ta = $row["sp_mo_ta"];
		$spDTO->sp_don_vi_tinh = $row["sp_don_vi_tinh"];
		$spDTO->sp_don_gia = $row["sp_don_gia"];
		$spDTO->sp_hinh_anh = $row["sp_hinh_anh"];
		$spDTO->sp_so_luong = $row["sp_so_luong"];
		$spDTO->sp_so_luong_ban = $row["sp_so_luong_ban"];
		$spDTO->sp_tinh_trang = $row["sp_tinh_trang"];
		$spDTO->sp_tong_luot_danh_gia = $row["sp_tong_luot_danh_gia"];
		$spDTO->sp_tong_so_sao = $row["sp_tong_so_sao"];
		$spDTO->sp_ma_danh_muc = $row["sp_ma_danh_muc"];
		$spDTO->sp_ten_danh_muc = $row["dm_ten"];
		$spDTO->sp_ma_gian_hang = $row["sp_ma_gian_hang"];
		$spDTO->sp_ten_gian_hang = $row["gh_ten"];
		$spDTO->sp_username_chu_giang_hang = $row["gh_username"];
		$spDTO->tg_ten_tag = $row["tg_ten_tag"];
		
		return $spDTO;
	}	
	public static function LaySamPhamTheoMaGianHang($maGianHang)
	{
		$sql = "SELECT * FROM san_pham WHERE sp_ma_gian_hang = '$maGianHang'";
		$result = QuerySQL($sql);
		
		$i = 0;
		while($row = mysql_fetch_array($result))
		{
			$spDTO = SanPhamDAO::PartseToSanPhamDTO($row);
			$arraySpDTO[$i] = $spDTO;
			$i = $i + 1;
		}
		StopConnection();
		return $arraySpDTO;

	}
	
	public static function ChiTietSanPham($sp_ma)
	{
		$sqlString = "
					select *
					from san_pham 
					left join gian_hang on sp_ma_gian_hang = gh_ma
					left join danh_muc on sp_ma_danh_muc = dm_ma
					left join tag on tg_ma_san_pham = sp_ma
					where sp_ma = '$sp_ma' and sp_tinh_trang = 1
					  ";
		$results = QuerySQL($sqlString);
		while($row = mysql_fetch_array($results))
		{
			$spDTO = SanPham::PartseToSanPham($row);
			StopConnection();
			return $spDTO;	
		}
				 
	}
	
	public static function DanhSachSanPhamTheoDanhMuc($madanhmuc)
	{
		$sqlString = "
					select *
					from san_pham 
					left join gian_hang on sp_ma_gian_hang = gh_ma
					left join danh_muc on sp_ma_danh_muc = dm_ma
					left join tag on tg_ma_san_pham = sp_ma
					where dm_ma = '$madanhmuc' and sp_tinh_trang = 1
					  ";
		$results = QuerySQL($sqlString);
		while($row = mysql_fetch_array($results))
		{
			$spDTO = SanPham::PartseToSanPham($row);
			StopConnection();
			return $spDTO;	
		}
				 
	}
	
	public static function CapNhatSanPham($spDTO)
	{
		$sqlString = "update san_pham set 		
						sp_don_gia = '$spDTO->sp_don_gia',
						sp_don_vi_tinh = '$spDTO->sp_don_vi_tinh',
						sp_so_luong = '$spDTO->sp_so_luong',
						sp_ma_danh_muc = '$spDTO->sp_ma_danh_muc',
						sp_mo_ta = '$spDTO->sp_mo_ta'
						WHERE sp_ma = '$spDTO->sp_ma'";	
		QuerySQL($sqlString);
		StopConnection();
	}
	
	public static function XoaSanPham($sp_ma)
	{
		$sqlString = "update san_pham set 		
						sp_tinh_trang = '0'
						WHERE sp_ma = '$sp_ma'";	
		QuerySQL($sqlString);
		StopConnection();
	}
	public static function ThemSanPhamMoi($sanpham)
	{
		$sqlString = "insert into san_pham (sp_ten, sp_ngay_tao, sp_mo_ta, sp_don_vi_tinh, sp_don_gia, sp_hinh_anh, sp_so_luong, sp_tinh_trang, sp_ma_gian_hang,sp_ma_danh_muc)  values( 						
						'$sanpham->sp_ten',
						'$sanpham->sp_ngay_tao',
						'$sanpham->sp_mo_ta',
						'$sanpham->sp_don_vi_tinh',
						'$sanpham->sp_don_gia',
						'$sanpham->sp_hinh_anh',
						'$sanpham->sp_so_luong',
						'$sanpham->sp_tinh_trang',
						'$sanpham->sp_ma_gian_hang',
						'$sanpham->sp_ma_danh_muc')";
		return InsertAndGetID($sqlString);
		StopConnection();
	}
	
	public static function KiemTraTenSanPham($TenSanPham)
	{
		$sqlString = "select *
					  from san_pham
					  where sp_ten = '$TenSanPham'
					  ";
		$results = QuerySQL($sqlString);
		$rows = mysql_num_rows($results);
		StopConnection();
		if($rows)
			return false;
		return true;
	}
	
	public static function SanPhamBanChay()
	{
		$sqlString = "
					select *
					from san_pham 
					left join gian_hang on sp_ma_gian_hang = gh_ma
					left join danh_muc on sp_ma_danh_muc = dm_ma
					left join tag on tg_ma_san_pham = sp_ma
					 where sp_tinh_trang = 1
					 order by sp_so_luong_ban desc
					 Limit 0, 10";
		$results = QuerySQL($sqlString);
		$i = 0;
		while($row = mysql_fetch_array($results))
		{
			$sp = SanPham::PartseToSanPham($row);
			$arraySp[$i] = $sp;
			$i = $i + 1;
		}
		StopConnection();
		return $arraySp;
	}
}
?>
